<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.sql.*"
    import="java.util.Calendar"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>歸還服務</title>
</head>

<body>
<% if(session.getAttribute("access") == "y" ){%>
<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
<jsp:useBean id="p_state" scope="page" class="bean.StateBean"/>
<table width="750" border="0" align="center">
  <tr>
    <td width="30" rowspan="4"><jsp:include page="../frameset/left.jsp" flush="true" ></jsp:include></td>
    <td colspan="2"><jsp:include page="../frameset/top.jsp" flush="true" ></jsp:include></td>
    <td width="40" rowspan="4"><jsp:include page="../frameset/right.jsp" flush="true" ></jsp:include></td>
  </tr>
  <tr>
    <td rowspan="2"><jsp:include page="../frameset/mean.jsp" flush="true" ></jsp:include>

    </td>
    <td height="32"><jsp:include page="../frameset/return_mean.jsp" flush="true" ></jsp:include></td>
  </tr>
  <tr>
    <td>
    <%--內頁--%>
    <%
    String date=""; 
    Calendar cal =Calendar.getInstance(); 
	String y1 = ""; //查詢開始年份 
   	String m1 = ""; //查詢開始月份 
   	String d1 = ""; //查詢開始日期 
    y1 = String.valueOf(cal.get(cal.YEAR)); 
    m1=String.valueOf(cal.get(cal.MONTH)+1); 
    d1 =String.valueOf(cal.get(cal.DATE));
    date=y1+"/"+m1+"/"+d1;
    %>
    <table width="500" border="0" height="200" align="center">
  <tr align="center">
    <td>
		<table width="550" border="0">
  			 <tr>
   				 <td colspan="6" align="center" bgcolor="#FFFF66">下列為<%=date %>為止應歸還的場地設備資料</td>
  			</tr>
  			<%
  			String sql="";
  			ResultSet rs;
  			ResultSet rs2;
  			int c_pf=0;
  			//先判斷借用資料裡,有哪些資料是今日需還,或是已經逾期的資料,將之列出
  			sql="select * from BORROW WHERE MUST_RETURN_DATE<='"+date+"' and RETURN_DATE is null";
			rs=mydb.executeQuery(sql);
			if(rs.next()){%>
			<tr>
    			<td>編號</td>
   				<td>名稱</td>
    			
    			<td>應還日期</td>
    			<td>借用者</td>
    			<td>狀態</td>
    			<td>歸還</td>
  			</tr>
  				<%do{ 
  					sql="select * from PLACE WHERE P_ID='"+rs.getString("PF_ID")+"'";
					rs2=mydb.executeQuery(sql);
  					if(rs2.next()){//為場地資料
  						c_pf=1;
  					}else{
  						sql="select * from FACILITY WHERE F_ID='"+rs.getString("PF_ID")+"'";
						rs2=mydb.executeQuery(sql);
  						if(rs2.next()){//為設備資料
  							c_pf=2;
  						}
  					}
  				%>
  			<tr><form action="check_bad.jsp" method="post">
    			<td><%=rs.getString("PF_ID")%><input type="hidden" name="pf_id" value="<%=rs.getString("PF_ID")%>"></td>
    			<td>
    			<%
    				if(c_pf==1){%>
    					<%=rs2.getString("P_NAME")%><input type="hidden" name="pf_name" value="<%=rs2.getString("P_NAME")%>"></td>
    				<%
    				}else if(c_pf==2){%>
    					<%=rs2.getString("F_NAME")%><input type="hidden" name="pf_name" value="<%=rs2.getString("F_NAME")%>"></td>
    				<%
    				}
    			 %>
    			
    			<td><%=(rs.getString("MUST_RETURN_DATE")).substring(0,10)%></td>
    			<td><%=rs.getString("M_ID")%><input type="hidden" name="m_id" value="<%=rs.getString("M_ID")%>"></td>
    			<td>
    			<%
    				if(c_pf==1){%>
    					<%=p_state.getPState(rs2.getString("P_STATE"))%>
    				<%
    				}else if(c_pf==2){%>
    					<%=p_state.getPState(rs2.getString("F_STATE"))%>
    				<%
    				}
    			 %>
    			</td>
    			<td>
    			<input type="hidden" name="must_rd" value="<%=rs.getString("MUST_RETURN_DATE")%>">
    			<input type="submit" name="Submit" value="歸還" /></td></form>
  			</tr>
			<%
				}while(rs.next());
			}else{%>
			<tr>
   				 <td colspan="6" align="center"><%=date %>沒有應歸還的場地設備資料</td>
  			</tr>
  			<%
			}
  			 %>
		</table>
    </td>
  </tr>
</table>
<%--內頁end--%>
    </td>
  </tr>
  <tr>
    <td colspan="2"></td>
  </tr>
</table>
<%
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>

</body>
</html>
